<template>
  <a-spin :spinning="cardLoading">
    <x-card v-if="hasPerm('course:page')">
      <div slot="content" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="20">
            <a-col :md="5" :sm="24">
              <a-form-item label="课程名称">
                <a-input v-model="queryParam.Title" allow-clear placeholder="请输入课程名称" />
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="record => record.id"
        :rowSelection="options.rowSelection"
      >
        <template slot="operator" v-if="hasPerm('course:add')">
          <a-button
            @click="$refs.addForm.add(courseTypeList, lecturerList)"
            icon="plus"
            type="primary"
            v-if="hasPerm('course:add')"
            >新增</a-button
          >
          <x-down style="display:none" v-if="hasPerm('course:export')" ref="batchExport" @batchExport="batchExport" />
        </template>
        <span slot="image" slot-scope="text, record">
          <span slot="image">
            <img class="preview-img" v-if="record.imgId" :src="imgTitle + record.imgId" alt="" />
          </span>
        </span>
        <span slot="isEnable" slot-scope="text, record">
          <span slot="isEnable">
            <span>{{ record.isShow ? '启用' : '禁用' }}</span>
          </span>
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('course:edit')" @click="$refs.editForm.edit(record, courseTypeList, lecturerList)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('course:edit') & hasPerm('course:play')" />
          <a v-if="hasPerm('course:play')" @click="bofang(record)">播放</a>
          <!-- <a-divider type="vertical" v-if="hasPerm('course:play') & hasPerm('course:changeenable')"/> -->
          <!-- <a v-if="hasPerm('course:changeenable')" @click="shezhizhuangtai(record)">{{record.isEnable?'禁用':'启用'}}</a> -->
          <a-divider type="vertical" v-if="hasPerm('course:play') & hasPerm('course:delete')" />
          <a-popconfirm
            v-if="hasPerm('course:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => singleDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>

    <a-modal :visible="previewVideoVisible" :footer="null" @cancel="handlePreviewVideoCancel">
      <video style="width: 100%; marginTop:20px;" :src="courseVideo" controls></video>
    </a-modal>
  </a-spin>
</template>

<script>
import { STable, XCard, XDown } from '@/components'
import {
  coursePage,
  courseDelete,
  lecturerList,
  courseTypeList,
  changeEnable,
  playCount
} from '@/api/modular/garden/courseManage'
import addForm from './addForm'
import editForm from './editForm'

export default {
  components: {
    XDown,
    XCard,
    STable,
    addForm,
    editForm
  },

  data() {
    return {
      // 查询参数
      queryParam: {
        Name: ''
      },
      cardLoading: false,
      // 表头
      columns: [
        {
          title: '缩略图',
          dataIndex: 'image',
          scopedSlots: { customRender: 'image' }
        },
        {
          title: '课程名称',
          dataIndex: 'courseName'
        },
        {
          title: '课程分类',
          dataIndex: 'courseTypeStr'
        },
        {
          title: '讲师',
          dataIndex: 'lecturerName'
        },
        {
          title: '时长/分',
          dataIndex: 'duration'
        },
        {
          title: '类型',
          dataIndex: 'coursesource'
        },
        {
          title: '状态',
          dataIndex: 'isEnable',
          scopedSlots: { customRender: 'isEnable' }
        },
        {
          title: '点播次数',
          dataIndex: 'playCount'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return coursePage(Object.assign(parameter, this.queryParam)).then(res => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      imgTitle: '',
      lecturerList: [],
      courseTypeList: [],
      previewVideoVisible: false,
      courseVideo: ''
    }
  },

  created() {
    this.imgTitle = process.env.VUE_APP_API_BASE_URL + '/sysFileInfo/previewImg?id='
    if (this.hasPerm('course:edit') || this.hasPerm('course:delete')) {
      this.columns.push({
        title: '操作',
        width: '220px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
    this.getlecturerList()
    this.getcourseTypeList()
  },

  methods: {
    getlecturerList() {
      lecturerList()
        .then(res => {
          if (res.success) {
            this.lecturerList = res.data
          } else {
            this.$message.error('拉取失败' + res.message)
          }
        })
        .catch(err => {
          this.$message.error('拉取错误' + err.message)
        })
    },
    getcourseTypeList() {
      courseTypeList()
        .then(res => {
          if (res.success) {
            this.courseTypeList = res.data
          } else {
            this.$message.error('拉取失败' + res.message)
          }
        })
        .catch(err => {
          this.$message.error('拉取错误' + err.message)
        })
    },
    shezhizhuangtai(record) {},
    bofang(record) {
      this.courseVideo = this.imgTitle + record.videosId
      this.previewVideoVisible = true
      const param = { id: record.id }
      playCount(param)
        .then(res => {
          this.$refs.table.clearRefreshSelected()
        })
        .catch(err => {})
    },
    handlePreviewVideoCancel() {
      this.previewVideoVisible = false
    },
    /**
     * 单个删除
     */
    singleDelete(record) {
      const param = [{ id: record.id }]
      this.sysPosDelete(param)
    },
    /**
     * 批量删除
     */
    batchDelete() {
      const paramIds = this.selectedRowKeys.map(d => {
        return { id: d }
      })
      this.sysPosDelete(paramIds)
    },
    /**
     * 删除
     */
    sysPosDelete(param) {
      courseDelete(param)
        .then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        })
        .catch(err => {
          this.$message.error('删除错误：' + err.message)
        })
    },
    changeStatus(record) {
      console.log(record)
    },
    /**
     * 批量导出
     */
    batchExport() {
      // const paramIds = this.selectedRowKeys.map((d) => {
      //   return { 'id': d }
      // })
      // sysPosExport(paramIds).then((res) => {
      //   this.$refs.batchExport.downloadfile(res)
      // })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>

<style lang="less" scoped>
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
.preview-img {
  display: inline-block;
  height: 50px;
  max-width: 120px;
}
</style>
